unit AfterDownloadSetupForm;

interface

uses
  AfterDownload_Defines,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, CheckLst, ExtCtrls;

type
  TAfDSetupForm = class(TForm)
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    GB_Log: TGroupBox;
    CB_LogEnabled: TCheckBox;
    SB_LogShow: TSpeedButton;
    CB_Matched: TCheckBox;
    CB_Unmatched: TCheckBox;
    CB_Actions: TCheckBox;
    CB_AErrors: TCheckBox;
    GB_Lang: TGroupBox;
    RB_byDM: TRadioButton;
    RB_Avail: TRadioButton;
    CBX_Lang: TComboBox;
    CB_FileSaved: TCheckBox;
    Label1: TLabel;
    CBX_LogDump: TComboBox;
    CB_LogDM: TCheckBox;
    GB_Rules: TGroupBox;
    SB1: TSpeedButton;
    SB2: TSpeedButton;
    SB3: TSpeedButton;
    SB4: TSpeedButton;
    CLB1: TCheckListBox;
    CB2: TCheckBox;
    SB_Top: TSpeedButton;
    SB_Bottom: TSpeedButton;
    GB_Default: TGroupBox;
    CB_RuleEnabled: TCheckBox;
    CB_ActionEnabled: TCheckBox;
    CB_RulesChanged: TCheckBox;
    RG_List: TRadioGroup;
    procedure SB2Click(Sender: TObject);
    procedure CLB1Click(Sender: TObject);
    procedure SB3Click(Sender: TObject);
    procedure SB4Click(Sender: TObject);
    procedure CLB1DblClick(Sender: TObject);
    procedure SB1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CLB1ClickCheck(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure CB_LogEnabledClick(Sender: TObject);
    procedure SB_LogShowClick(Sender: TObject);
    procedure CBX_LangChange(Sender: TObject);
    procedure RB_byDMClick(Sender: TObject);
    procedure RB_AvailClick(Sender: TObject);
    procedure CB_LogDMClick(Sender: TObject);
    procedure CBX_LogDumpChange(Sender: TObject);
    procedure SB_TopClick(Sender: TObject);
    procedure SB_BottomClick(Sender: TObject);
    procedure CB_RuleEnabledClick(Sender: TObject);
    procedure CB_ActionEnabledClick(Sender: TObject);
    procedure RG_ListClick(Sender: TObject);
  private
    { Private declarations }
    Lang: String;
    FRules: RulesCollection;
    sWantRemoveRule: String;
    procedure TranslateToo;
    procedure SetLanguage(language: String);
    procedure UpdateRules(SetIndex: Integer = -1);
    function EditRule(Idx: Integer): Boolean;
    procedure CheckButtons;
    procedure SetRules(ARules: RulesCollection);
    procedure BitBtnClick(BitBtn: TBitBtn);
    procedure CheckLog;
    procedure CheckLang;
  public
    { Public declarations }
    property Language: String write SetLanguage;
    property formRules: RulesCollection read FRules write SetRules;
  end;

var
  ADSF: TAfDSetupForm = nil;

implementation

{$R *.dfm}
uses
 gnugettext,
 languagecodes,
 Locales,
 Wizard,
 AfterDownload_Log,
 AfterDownload_RuleForm;
 //AfterDownloadRuleForm;

procedure TAfDSetupForm.SetLanguage(language: String);
begin
 Lang:=language;
 TranslateForm(Self, Language, TranslateToo);
end;

procedure TAfDSetupForm.UpdateRules;
 var
  i, j: Integer;
begin
 CLB1.Items.Clear;
 for i:=0 to FRules.Rules.Count-1 do
  begin
   j:=CLB1.Items.Add(FRules.Rules[i].Title);
   CLB1.Checked[j]:=FRules.Rules[i].Enabled;
  end;
 if (SetIndex>=0) and (SetIndex<CLB1.Items.Count) then
   CLB1.ItemIndex:=SetIndex;
 CheckButtons;
end;

procedure TAfDSetupForm.SB2Click(Sender: TObject);
 var
  i: Integer;
begin
 i:=CLB1.ItemIndex;
 if (i>=0)
     and (MessageDlg(Format(sWantRemoveRule, [FRules.Rules[i].Title]), mtConfirmation, [mbYes, mbNo], 0)=mrYes) then
  begin
   FRules.Rules.Delete(i);
   UpdateRules;
  end;
end;

procedure TAfDSetupForm.CheckButtons;
begin
 SB2.Enabled:=CLB1.ItemIndex>=0;
 SB3.Enabled:=CLB1.ItemIndex>0;
 SB4.Enabled:=SB2.Enabled and (CLB1.ItemIndex<CLB1.Items.Count-1);
 SB_Top.Enabled:=CLB1.ItemIndex>0;
 SB_Bottom.Enabled:=CLB1.ItemIndex<CLB1.Items.Count-1;
end;

procedure TAfDSetupForm.CLB1Click(Sender: TObject);
begin
 CheckButtons;
end;

procedure TAfDSetupForm.SB3Click(Sender: TObject);
 var
  i: Integer;
begin
 i:=CLB1.ItemIndex;
 if i>0 then
  begin
   FRules.Rules.Items[i].Index:=i-1;
   UpdateRules(i-1);
  end;
end;

procedure TAfDSetupForm.SB4Click(Sender: TObject);
 var
  i: Integer;
begin
 i:=CLB1.ItemIndex;
 if i<FRules.Rules.Count-1 then
  begin
   FRules.Rules.Items[i].Index:=i+1;
   UpdateRules(i+1);
  end;
end;

procedure TAfDSetupForm.CLB1DblClick(Sender: TObject);
begin
 if (CLB1.ItemIndex>=0) and EditRule(CLB1.ItemIndex) then
   UpdateRules(CLB1.ItemIndex);
end;

function TAfDSetupForm.EditRule(Idx: Integer): Boolean;
 var
  New: Boolean;
begin
 AD_RF:=TAD_RuleForm.Create(Self);
 AD_RF.Language:=Lang;
 if (Idx>=0) then
  begin
   if Idx=FRules.Rules.Count then
    begin
     AD_RF.myRule:=FRules.Rules.Add(myFunction);
     New:=true;
    end
   else
    begin
     AD_RF.myRule:=FRules.Rules[Idx];
     New:=false;
    end;
   AD_RF.ActionByDefault:=FRules.ActionByDefault; 
   Result:=AD_RF.ShowModal=mrOK;
   if Result then
    begin
     FRules.Rules[Idx].Assign(AD_RF.myRule);
     if New then
       FRules.Rules[Idx].Enabled:=FRules.RuleByDefault;
    end
   else
    begin
     if New then
       FRules.Rules.Delete(FRules.Rules.Count-1);
    end;
   FreeAndNil(AD_RF);
  end
 else
   Result:=false;
end;

procedure TAfDSetupForm.SB1Click(Sender: TObject);
begin
 if EditRule(FRules.Rules.Count) then
   UpdateRules(FRules.Rules.Count-1);
end;

procedure TAfDSetupForm.FormCreate(Sender: TObject);
begin
 TranslateComponent(Self);
 //
 FRules:=RulesCollection.Create(nil);
 //
 myLocales.GetLangNames(CBX_Lang.Items);
 //
 SB2.Enabled:=false;
 SB3.Enabled:=false;
 SB4.Enabled:=false;
end;

procedure TAfDSetupForm.CLB1ClickCheck(Sender: TObject);
 var
  i: Integer;
begin
 i:=CLB1.ItemIndex;
 if i>=0 then
   FRules.Rules[i].Enabled:=CLB1.Checked[i];
end;

procedure TAfDSetupForm.FormDestroy(Sender: TObject);
begin
 FRules.Free;
end;

procedure TAfDSetupForm.SetRules(ARules: RulesCollection);
begin
 //ShowMessage('Before assign'); //debug
 FRules.Assign(ARules);
 //ShowMessage('After assign'); //debug
 //
 CB2.Checked:=FRules.Silent;
 CB_RuleEnabled.Checked:=FRules.RuleByDefault;
 CB_ActionEnabled.Checked:=FRules.ActionByDefault;
 CheckLog;
 CheckLang;
 UpdateRules;
 RG_List.ItemIndex:=FRules.SavedReturn;
end;

procedure TAfDSetupForm.TranslateToo;
begin
 sWantRemoveRule:=_('Are you sure to delete rule %s ?');
 BitBtn2.Caption:=_('Cancel');
end;

procedure TAfDSetupForm.BitBtnClick(BitBtn: TBitBtn);
begin
 Self.ModalResult:=BitBtn.ModalResult;
 if not (fsModal in Self.FormState) then
   Self.Close;
end;

procedure TAfDSetupForm.BitBtn1Click(Sender: TObject);
begin
 BitBtnClick(BitBtn1);
end;

procedure TAfDSetupForm.BitBtn2Click(Sender: TObject);
begin
 BitBtnClick(BitBtn2);
end;

procedure TAfDSetupForm.CheckLog;
begin
 CB_LogEnabled.Checked:=IsBitSet(FRules.Log, cLogUse);
 SB_LogShow.Enabled:=CB_LogEnabled.Checked and LogActive;
 CB_FileSaved.Enabled:=CB_LogEnabled.Checked;
 CB_FileSaved.Checked:=IsBitSet(FRules.Log, cLogFileSaved);
 CB_Matched.Enabled:=CB_LogEnabled.Checked;
 CB_Matched.Checked:=IsBitSet(FRules.Log, cLogMatched);
 CB_Unmatched.Enabled:=CB_LogEnabled.Checked;
 CB_Unmatched.Checked:=IsBitSet(FRules.Log, cLogUnmatched);
 CB_RulesChanged.Enabled:=CB_LogEnabled.Checked;
 CB_RulesChanged.Checked:=IsBitSet(FRules.Log, cLogChanged);
 CB_Actions.Enabled:=CB_LogEnabled.Checked;
 CB_Actions.Checked:=IsBitSet(FRules.Log, cLogActions);
 CB_AErrors.Enabled:=CB_LogEnabled.Checked;
 CB_AErrors.Checked:=IsBitSet(FRules.Log, cLogActionErrors);
 Label1.Enabled:=CB_LogEnabled.Checked;
 CBX_LogDump.Enabled:=CB_LogEnabled.Checked;
 CB_LogDM.Enabled:=CB_LogEnabled.Checked;
 CBX_LogDump.ItemIndex:=FRules.LogDump;
 if CBX_LogDump.ItemIndex<0 then
  begin
   CBX_LogDump.ItemIndex:=cTimeExit;
   FRules.LogDump:=cTimeExit;
  end;
 CB_LogDM.Checked:=FRules.LogDM; 
end;

procedure TAfDSetupForm.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
 FRules.Log:=0;
 if CB_LogEnabled.Checked then
   FRules.Log:=FRules.Log or cLogUse;
 if CB_FileSaved.Checked then
   FRules.Log:=FRules.Log or cLogFileSaved;
 if CB_Matched.Checked then
   FRules.Log:=FRules.Log or cLogMatched;
 if CB_Unmatched.Checked then
   FRules.Log:=FRules.Log or cLogUnmatched;
 if CB_RulesChanged.Checked then
   FRules.Log:=FRules.Log or cLogChanged;
 if CB_Actions.Checked then
   FRules.Log:=FRules.Log or cLogActions;
 if CB_AErrors.Checked then
   FRules.Log:=FRules.Log or cLogActionErrors;
 //
 FRules.Silent:=CB2.Checked;
 FRules.RuleByDefault:=CB_RuleEnabled.Checked;
 FRules.ActionByDefault:=CB_ActionEnabled.Checked;
end;

procedure TAfDSetupForm.CB_LogEnabledClick(Sender: TObject);
begin
 if CB_LogEnabled.Checked then
   FRules.Log:=FRules.Log or cLogUse
 else
   FRules.Log:=FRules.Log and cLogAll;
 CheckLog;
end;

procedure TAfDSetupForm.SB_LogShowClick(Sender: TObject);
begin
 LogShow(LogTitle);
end;

procedure TAfDSetupForm.CheckLang;
 var
  i: Integer;
begin
 RB_byDM.Checked:=FRules.LangMode=lmByDM;
 RB_Avail.Checked:=FRules.LangMode=lmAvail;
 CBX_Lang.Enabled:=RB_Avail.Checked;
 if CBX_Lang.Enabled then
  begin
   if getlanguagename(FRules.LangCode)='' then
     i:=0
   else
    begin
     i:=myLocales.IndexOfName(FRules.LangCode);
     if i<0 then
       i:=0;
    end;
   CBX_Lang.ItemIndex:=i;
   if i=0 then
     FRules.LangCode:='en';
  end
 else
   CBX_Lang.ItemIndex:=0;
end;

procedure TAfDSetupForm.CBX_LangChange(Sender: TObject);
begin
 FRules.LangCode:=myLocales.Names[CBX_Lang.ItemIndex];
end;

procedure TAfDSetupForm.RB_byDMClick(Sender: TObject);
begin
 FRules.LangMode:=lmByDM;
 CheckLang;
end;

procedure TAfDSetupForm.RB_AvailClick(Sender: TObject);
begin
 FRules.LangMode:=lmAvail;
 CheckLang;
end;

procedure TAfDSetupForm.CB_LogDMClick(Sender: TObject);
begin
 FRules.LogDM:=CB_LogDM.Checked;
end;

procedure TAfDSetupForm.CBX_LogDumpChange(Sender: TObject);
begin
 FRules.LogDump:=CBX_LogDump.ItemIndex;
end;

procedure TAfDSetupForm.SB_TopClick(Sender: TObject);
 var
  i: Integer;
begin
 i:=CLB1.ItemIndex;
 if i>0 then
  begin
   FRules.Rules.Items[i].Index:=0;
   UpdateRules(0);
  end;
end;

procedure TAfDSetupForm.SB_BottomClick(Sender: TObject);
 var
  i, c: Integer;
begin
 c:=FRules.Rules.Count-1;
 i:=CLB1.ItemIndex;
 if i<c then
  begin
   FRules.Rules.Items[i].Index:=c;
   UpdateRules(c);
  end;
end;

procedure TAfDSetupForm.CB_RuleEnabledClick(Sender: TObject);
begin
 FRules.RuleByDefault:=CB_RuleEnabled.Checked;
end;

procedure TAfDSetupForm.CB_ActionEnabledClick(Sender: TObject);
begin
 FRules.ActionByDefault:=CB_ActionEnabled.Checked;
end;

procedure TAfDSetupForm.RG_ListClick(Sender: TObject);
begin
 FRules.SavedReturn:=RG_List.ItemIndex;
end;

end.
